package lwf.web.struts.action;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lwf.web.struts.action.form.LoginActionForm;
import net.lwf.entity.WebUser;
import net.lwf.hibernate.entity.User;
import net.lwf.service.AppHitCounter;
import net.lwf.service.UserService;
import net.lwf.util.SessionData;
import net.lwf.util.web.WebUtil;
import net.lwf.web.constant.IWebConstant;
import net.lwf.web.struts.action.UserDataHelper;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.actions.DispatchAction;

public class LoginAction extends DispatchAction {
	
	private static final Logger logger = Logger.getLogger(lwf.web.struts.action.LoginAction.class);
	
	public ActionForward display(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return mapping.findForward("display");
	}
	
	public ActionForward guestlogin(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		LoginActionForm lform = (LoginActionForm) form;
		User user = new User();
		user.setGrade(lform.getSelectedGrade());
		WebUser webuser = new WebUser(user);
		UserDataHelper.setBaseUserInfo(request, webuser);
		return mapping.findForward("display");
	}
	
	public ActionForward login(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		LoginActionForm lform = (LoginActionForm) form;
		String userid = lform.getUserid();
		String password = lform.getPassword();
		
		if(userid != null && password != null) {
			User user = this.getUserService().getUser(userid, password);
			if(user != null && user.isValid()) {
				WebUser webuser = new WebUser(user);
				UserDataHelper.setBaseUserInfo(request, webuser);
				//Add the hit counter
				AppHitCounter.increaseCounter();
				return mapping.findForward("success");						
			}
			else {
				ActionMessages messages = new ActionMessages();
				messages.add("globalmessage", new ActionMessage("invalid.user"));
				saveErrors(request, messages);
			}
		}
		
		return mapping.findForward("display");
	}

	public ActionForward logout(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String username = "";
		WebUser user = this.getUser(request);
		if(user != null) {
			username = user.getUser().getUserName();
			logger.debug("===================> User: " + username + " logged OUT successfully at " + (new Date()).toString());
		}
		
		request.setAttribute(IWebConstant.USER_NAME, username);

		SessionData.invalidateData(request);
		
		return mapping.findForward("thankyou");

	}
	
	private WebUser getUser(HttpServletRequest request) {
		WebUser user = (WebUser) WebUtil.getAttribute(request, IWebConstant.WEB_USER);
		if(user != null) {
			return user;
		}
		return null;
	}
	
	private UserService getUserService() {
		return new UserService();
	}
	
} //LoginAction
